/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.wechatmsg.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinGroup;
import com.lczyfz.campusinfo.modules.wechatmsg.service.WeixinGroupService;

/**
 * 微信组别管理Controller
 * @author 陈维乾
 * @version 2018-05-06
 */
@Controller
@RequestMapping(value = "${adminPath}/wechatmsg/weixinGroup")
public class WeixinGroupController extends BaseController {

	@Autowired
	private WeixinGroupService weixinGroupService;
	
	@ModelAttribute
	public WeixinGroup get(@RequestParam(required=false) String id) {
		WeixinGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = weixinGroupService.get(id);
		}
		if (entity == null){
			entity = new WeixinGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("wechatmsg:weixinGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeixinGroup weixinGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WeixinGroup> page = weixinGroupService.findPage(new Page<WeixinGroup>(request, response), weixinGroup); 
		model.addAttribute("page", page);
		return "modules/wechatmsg/weixinGroupList";
	}

	@RequiresPermissions("wechatmsg:weixinGroup:view")
	@RequestMapping(value = "form")
	public String form(WeixinGroup weixinGroup, Model model) {
		model.addAttribute("weixinGroup", weixinGroup);
		return "modules/wechatmsg/weixinGroupForm";
	}

	@RequiresPermissions("wechatmsg:weixinGroup:edit")
	@RequestMapping(value = "save")
	public String save(WeixinGroup weixinGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, weixinGroup)){
			return form(weixinGroup, model);
		}
		weixinGroupService.save(weixinGroup);
		addMessage(redirectAttributes, "保存微信组别成功");
		return "redirect:"+Global.getAdminPath()+"/wechatmsg/weixinGroup/?repage";
	}
	
	@RequiresPermissions("wechatmsg:weixinGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(WeixinGroup weixinGroup, RedirectAttributes redirectAttributes) {
		weixinGroupService.delete(weixinGroup);
		addMessage(redirectAttributes, "删除微信组别成功");
		return "redirect:"+Global.getAdminPath()+"/wechatmsg/weixinGroup/?repage";
	}

}